package com.workingpub.commons.jdbc.mapping;

import java.sql.ResultSet;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.workingpub.commons.utils.DateFormatUtil;

/**
 * Map集合映射
 * 
 * @author taoping
 *
 * @param <T>
 */
public class MapMapping<T> extends AbstractRowMapping<T> {

	@Override
	protected T mapping(Class<T> resultType, ResultSet rs, int rowNum) throws Exception {
		if (!Map.class.isAssignableFrom(resultType)) {
			return null;
		}
		Map<String, Object> result = new HashMap<String, Object>();
		// TODO 引用CommonConfig的全局日期格式化处理日期类型
		// TODO 考虑在引用字典或元数据时，针对字段的扩展
		for (Entry<String, Integer> entry : getColumnInfos().entrySet()) {
			Object value = null;
			if (Types.DATE == entry.getValue()) {
				value = DateFormatUtil.formatDate(rs.getDate(entry.getKey()));
			} else if (Types.TIMESTAMP == entry.getValue()) {
				value = DateFormatUtil.formateDatetime(rs.getTimestamp(entry.getKey()));
			} else if (Types.TIME == entry.getValue()) {
				value = DateFormatUtil.formateTime(rs.getTime(entry.getKey()));
			} else {
				value = convert(resultType, rs, entry.getKey());
			}
			result.put(entry.getKey(), value);
		}
		return resultType.cast(result);
	}
}
